package com.yonyou.pmclouds.plan.processor;

import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.util.DateUtils;
import com.yonyou.pmclouds.plan.entity.ProjectPlanVO;
import com.yonyou.pmclouds.plan.entity.ScheduleInsertArgsVO;
import com.yonyou.pmclouds.plan.entity.ScheduleItemVO;
import com.yonyou.pmclouds.plan.entity.ScheduleVO;
import com.yonyou.pmclouds.plan.planindex.entity.PlanIndexVO;
import com.yonyou.pmclouds.plan.rmiitf.IScheduleItemProcessor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Description: 计划任务节点处理类
 * @Author: zhangzhonghao
 * @Date: 2020-03-06
 */

@Slf4j
@Component
public class ScheduleItemInsertDefaultValueProcessor implements IScheduleItemProcessor {

    private List<ScheduleItemVO> insertScheduleItemList = new ArrayList<>();

    private List<ScheduleItemVO> updateScheduleItemList = new ArrayList<>();

    private Map<String, String> codeWithPKMap = new HashMap<>();

    private List<PlanIndexVO> insertScheduleIndexList = new ArrayList<>();

    private List<String> deleteScheduleIndexList = new ArrayList<>();

    @Override
    public void process(ScheduleVO scheduleVO, ScheduleItemVO rootParentItemVO, ScheduleItemVO parentItemVO,
                        ScheduleItemVO childItemVO) throws BusinessException {

        boolean isInsert = StringUtils.isEmpty(childItemVO.getPkScheduleItem());

        if(isInsert){
            insertScheduleItemList.add(childItemVO);
            setInsertDefault(scheduleVO, rootParentItemVO, parentItemVO, childItemVO);
        }else {
            updateScheduleItemList.add(childItemVO);
            // 如果修改时删除前置计划，则将前置主键置为空
            if(StringUtils.isEmpty(childItemVO.getDependPlanName())){
                childItemVO.setDependPlan("");
            }
        }

        codeWithPKMap.put(childItemVO.getCode(), childItemVO.getPkScheduleItem());

        childItemVO.setChildCount(childItemVO.getChildren() == null ? 0 : childItemVO.getChildren().length);

        levelNodeHandle(scheduleVO, childItemVO);

        getScheduleIndexList(scheduleVO, childItemVO);
    }

    private void setInsertDefault(ScheduleVO scheduleVO, ScheduleItemVO rootParentItemVO, ScheduleItemVO parentItemVO,
                                  ScheduleItemVO childItemVO){
        String pkScheduleItem = UUID.randomUUID().toString();
        childItemVO.setVersion(1);
        childItemVO.setPkScheduleItem(pkScheduleItem);
        childItemVO.setPkSchedule(scheduleVO.getPkSchedule());
        childItemVO.setPkOriginal(pkScheduleItem);
        childItemVO.setPkProject(scheduleVO.getPkProject());

        if(parentItemVO == null){
            // 根结点
            childItemVO.setPkRoot(pkScheduleItem);
        }else {
            childItemVO.setPkRoot(rootParentItemVO.getPkScheduleItem());
            childItemVO.setPkParent(parentItemVO.getPkScheduleItem());
        }

        if(StringUtils.isNotBlank(childItemVO.getCode())){
            String[] codes = childItemVO.getCode().split("\\.");
            if(codes.length > 0) childItemVO.setSeq(Integer.parseInt(codes[codes.length - 1]));
        }
    }

    private void levelNodeHandle(ScheduleVO scheduleVO, ScheduleItemVO childItemVO) throws BusinessException {
        if(childItemVO.getChildren() != null && childItemVO.getChildren().length > 0){
            return;
        }

        Date childPlanStartDate = DateUtils.asDateBegin(DateUtils.formatDate(childItemVO.getPlanStartDate()));
        Date childPlanEndDate = DateUtils.asDateEnd(DateUtils.formatDate(childItemVO.getPlanEndDate()));

        childItemVO.setPlanStartDate(childPlanStartDate);
        childItemVO.setPlanEndDate(childPlanEndDate);
        childItemVO.setPlanDays((int)DateUtils.getDaysBetween(childPlanStartDate, childPlanEndDate));

    }

    private void getScheduleIndexList(ScheduleVO scheduleVO, ScheduleItemVO childItemVO){
        if(childItemVO.getPlanIndexVOs() == null || childItemVO.getPlanIndexVOs().size() <= 0){
            childItemVO.setIndexNumber(0);
        }else{
            childItemVO.setIndexNumber(childItemVO.getPlanIndexVOs().size());
        }

        List<PlanIndexVO> planIndexVOList = childItemVO.getPlanIndexVOs();
        if(planIndexVOList != null && planIndexVOList.size() > 0){
            for(PlanIndexVO indexVO : planIndexVOList){
                indexVO.setPkPlan(childItemVO.getPkScheduleItem());
                indexVO.setPkProject(scheduleVO.getPkProject());
                insertScheduleIndexList.add(indexVO);
            }
        }

        if(childItemVO.getDeletePlanIndexes() != null && childItemVO.getDeletePlanIndexes().size() > 0){
            deleteScheduleIndexList.addAll(childItemVO.getDeletePlanIndexes());
        }
    }

    public List<ScheduleItemVO> getInsertScheduleItemList() {
        return insertScheduleItemList;
    }

    public List<ScheduleItemVO> getUpdateScheduleItemList() {
        return updateScheduleItemList;
    }

    public List<PlanIndexVO> getInsertScheduleIndexList() {
        return insertScheduleIndexList;
    }

    public Map<String, String> getCodeWithPKMap() {
        return codeWithPKMap;
    }

    public List<String> getDeleteScheduleIndexList() {
        return deleteScheduleIndexList;
    }
}
